﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FATCASchema.Nodes
{
    public class ReportingGroupNode : iNode
    {
        public string Name { get; private set; }
        public bool AllowAddChildren { get; private set; }
        public List<iNode> Children { get; private set; }
        public object NodeObject { get; private set; }

        public ReportingGroupNode(Fatca_TypeReportingGroup nodeObject)
        {
            this.Name = "Reporting Group";
            this.NodeObject = nodeObject;
            this.AllowAddChildren = false;
            this.LoadChildren();
        }

        public void LoadChildren()
        {
            if (this.NodeObject == null)
                return;

            var nodeObject = (Fatca_TypeReportingGroup)this.NodeObject;
            this.Children = new List<iNode>();
            
            if (nodeObject.Sponsor != null)
                this.Children.Add(new CorrectableOrganisationParty("Sponsor", nodeObject.Sponsor));

            if (nodeObject.Intermediary != null)
                this.Children.Add(new CorrectableOrganisationParty("Intermediary", nodeObject.Intermediary));

            if (nodeObject.AccountReport != null)
            {
                foreach (var item in nodeObject.AccountReport)
                {
                    this.Children.Add(new AccountReportNode(item));
                }
            }

            if (nodeObject.PoolReport != null)
            {
                foreach (var item in nodeObject.PoolReport)
                {
                    this.Children.Add(new PoolReportNode(item));
                }
            }
        }

        public iNode AddSponsor()
        {
            if (this.NodeObject == null)
                return null;

            var nodeObject = (Fatca_TypeReportingGroup)this.NodeObject;
            nodeObject.Sponsor = new CorrectableOrganisationParty_Type();
            nodeObject.Sponsor.DocSpec = new DocSpec_Type();
            nodeObject.Sponsor.Name = new NameOrganisation_Type[] { new NameOrganisation_Type()};

            var sponsorNode = new CorrectableOrganisationParty("Sponsor", nodeObject.Sponsor);
            this.Children.Add(sponsorNode);

            return sponsorNode;
        }

        public iNode AddIntermediary()
        {
            if (this.NodeObject == null)
                return null;

            var nodeObject = (Fatca_TypeReportingGroup)this.NodeObject;
            nodeObject.Intermediary = new CorrectableOrganisationParty_Type();
            nodeObject.Intermediary.DocSpec = new DocSpec_Type();
            nodeObject.Intermediary.Name = new NameOrganisation_Type[] { new NameOrganisation_Type() };

            var intermediaryNode = new CorrectableOrganisationParty("Intermediary", nodeObject.Intermediary);
            this.Children.Add(intermediaryNode);

            return intermediaryNode;
        }

        public iNode AddAccountReport()
        {
            if (this.NodeObject == null)
                return null;

            var nodeObject = (Fatca_TypeReportingGroup)this.NodeObject;

            var accountReport = new CorrectableAccountReport_Type();
            accountReport.DocSpec = new DocSpec_Type();

            List<CorrectableAccountReport_Type> reports = null;
            if (nodeObject.AccountReport == null)
                reports = new List<CorrectableAccountReport_Type>();
            else
                reports = nodeObject.AccountReport.ToList();

            reports.Add(accountReport);
            nodeObject.AccountReport = reports.ToArray();

            var accountNode = new AccountReportNode(accountReport);
            this.Children.Add(accountNode);

            return accountNode;
        }

        public iNode AddPoolReport()
        {
            if (this.NodeObject == null)
                return null;

            var nodeObject = (Fatca_TypeReportingGroup)this.NodeObject;

            var accountReport = new CorrectablePoolReport_Type();
            accountReport.PoolBalance = new MonAmnt_Type();
            accountReport.DocSpec = new DocSpec_Type();

            List<CorrectablePoolReport_Type> reports = null;
            if (nodeObject.PoolReport == null)
                reports = new List<CorrectablePoolReport_Type>();
            else
                reports = nodeObject.PoolReport.ToList();

            reports.Add(accountReport);
            nodeObject.PoolReport = reports.ToArray();

            var accountNode = new PoolReportNode(accountReport);
            this.Children.Add(accountNode);

            return accountNode;
        }

        public void AddChild()
        { }

        public void LoadPane()
        { }
    }
}
